#include "tools/linkstack.h"
#include "headers.h"

int initLStack_7th(LStack_7th *link_stack) {
	if (link_stack == NULL)
		return -1;
	link_stack->fail = NULL;
	link_stack->size = 0;
	return 0;
}

// 压栈
int pushLStack_7th(LStack_7th *stack,DT_7th *item) {
    if (stack==NULL||item==NULL)
        return -1;
    DT_7th *new_item=calloc(1,sizeof(DT_7th));
    new_item->dir_item=calloc(strlen(item->dir_item)+1,sizeof(char));
    memcpy(new_item->dir_item,item->dir_item,strlen(item->dir_item));


    new_item->netx=stack->fail;
    stack->fail=new_item;
    stack->size++;
    return 0;
}

// 出站
int pullLStack_7th(LStack_7th *stack,DT_7th *item){
    if(stack==NULL||item==NULL||stack->size<=0)
        return -1;
    if(item->dir_item)
    freeDTitem_7th(item);
    item->dir_item=calloc(strlen(stack->fail->dir_item)+1,sizeof(char));
    memcpy(item->dir_item,stack->fail->dir_item,strlen(stack->fail->dir_item));

    item->netx=NULL;
    DT_7th *it=stack->fail;
    stack->fail=it->netx;
    freeDT_7th(it);
    stack->size--;
    if(stack->size==0)
        stack->fail=NULL;
    return 0;
}


// 销毁itemtype
void freeDT_7th(DT_7th *item){
    if(item!=NULL)
    {
        if(item->dir_item)
            free(item->dir_item);
        free(item);
    }
}

// 销毁item字段
void freeDTitem_7th(DT_7th *item){
    if(item&&item->dir_item)
    free(item->dir_item);
}

// 销毁栈
void freeLSTack_7th(LStack_7th *stack){
    if(stack){
        while (stack->size&&stack->fail) {
            DT_7th *it=stack->fail;
            stack->fail=it->netx;
            freeDT_7th(it);
        }
    }
}
